local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local timer = require "timer"
local timetool = require "timetool"
local configdao = require "configdao"
local base = require "base"
local msgproxy = require "msgproxy"
local logicmng = require "logicmng"
local tabletool = require "tabletool"
local filename = "tablerequest.lua"
local nncardtool = require "nncardtool"

require "enum"

local TableRequest = {}

function TableRequest.process(session, source, event, ...)
	local f = TableRequest[event] 
	if f == nil then
		filelog.sys_error(filename.." TableRequest.process invalid event:"..event)
		base.skynet_retpack(nil)
        return nil
	end
	f(...)
end

function TableRequest.playerleave(request)
	local result
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")

	seat = roomtablelogic.get_seat_by_rid(table_data, request.rid)

	if seat == nil then
		base.skynet_retpack(false)		
		return		
	end
	base.skynet_retpack(true)
	
	roomtablelogic.playerleave(table_data, request, seat)
end

function TableRequest.disconnect(request)
	local result
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	if request.id ~= table_data.id then
		base.skynet_retpack(false)		
		return
	end

	seat = roomtablelogic.get_seat_by_rid(table_data, request.rid)

	if seat == nil then
		local waitinfo = table_data.waits[request.rid]
		if waitinfo ~= nil then
			roomtablelogic.passive_leavetable(table_data, request.rid, false)
		end
		base.skynet_retpack(false)		
		return		
	end

	if table_data.is_had_start and table_data.state == ENNTableState.TABLE_STATE_UNKNOW then
		roomtablelogic.passive_leavetable(table_data, seat.rid, false)
		base.skynet_retpack(false)		
		return				
	end

	if seat.gatesvr_id ~= request.gatesvr_id 
		or seat.agent_address ~= request.agent_address then
		base.skynet_retpack(false)		
		return		
	end
	base.skynet_retpack(true)
	
	roomtablelogic.disconnect(table_data, request, seat)
end
--[[

//请求进入桌子
message EnterTableReq {
	optional Version version = 1;
	optional int32 id = 2;
	optional string roomsvr_id = 3; //房间服务器id
	optional int32  roomsvr_table_address = 4; //桌子的服务器地址
	optional int32  gameid = 5; 
}

//响应进入桌子
message EnterTableRes {
	optional int32 errcode = 1; //错误原因 0表示成功
	optional string errcodedes = 2; //错误描述	
	optional GameInfo gameinfo = 3;
	optional int32 gameid = 4;
}

]]
function TableRequest.entertable(request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid, 
	}
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seatinfo, seat
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	
	if table_data.conf == nil 
		or request.id ~= table_data.id 
		or request.gameid ~= table_data.conf.gameid  then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效请求！"
		base.skynet_retpack(responsemsg, nil)		
		return
	end

	if table_data.conf.max_watch_playernum ~= nil 
		and table_data.cur_watch_playernum >= table_data.conf.max_watch_playernum then
		responsemsg.errcode = EErrCode.ERR_TABLE_FULL
		responsemsg.errcodedes = " 旁观人数过多!!!"
		base.skynet_retpack(responsemsg, nil)
		return 
	end

	if table_data.sitdown_player_num >= table_data.conf.max_player_num then
		responsemsg.errcode = EErrCode.ERR_TABLE_FULL
		responsemsg.errcodedes = " 当前桌子已满!!!"
		base.skynet_retpack(responsemsg, nil)
		return 		
	end

	if table_data.is_had_start 
		and table_data.state ~= ENNTableState.TABLE_STATE_UNKNOW then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "游戏已经开始不能再进入房间"
		base.skynet_retpack(responsemsg, nil)		
		return		
	end

	if table_data.is_had_start 
		and table_data.state == ENNTableState.TABLE_STATE_UNKNOW then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "游戏已经结束不能再进入房间"
		base.skynet_retpack(responsemsg, nil)		
		return		
	end

	if  table_data.conf.isaapay ~= nil 
		and table_data.conf.isaapay == 1 
		and table_data.conf.gemstonenum > request.gemstone then
		responsemsg.errcode = EErrCode.ERR_NOT_ENOUGH_GEMSTONE
		responsemsg.errcodedes = "金豆不够不能进入房间"
		base.skynet_retpack(responsemsg, nil)
		return 		
	end

	seat = roomtablelogic.get_seat_by_rid(table_data, request.rid)

	if seat ~= nil then
		seatinfo = {
			index = seat.index,
		}
		seat.gatesvr_id=request.gatesvr_id
		seat.agent_address = request.agent_address
		seat.playerinfo.rolename=request.playerinfo.rolename
		seat.playerinfo.logo=request.playerinfo.logo
		seat.playerinfo.sex=request.playerinfo.sex
		seat.playerinfo.ip = request.playerinfo.ip
	end

	responsemsg.gameinfo = {}

	msghelper:copy_table_gameinfo(responsemsg.gameinfo)
	base.skynet_retpack(responsemsg, seatinfo)
	roomtablelogic.entertable(table_data, request, seat)
end

function TableRequest.reentertable(request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid, 
	}
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seatinfo, seat, waitinfo
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")

	if table_data.conf == nil 
		or table_data.conf.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效请求！"
		base.skynet_retpack(responsemsg, nil)		
		return
	end

	seat = roomtablelogic.get_seat_by_rid(table_data, request.rid)
    waitinfo = table_data.waits[request.rid]
	if seat ~= nil then
		seatinfo = {
			index = seat.index,
		}
		seat.gatesvr_id=request.gatesvr_id
		seat.agent_address = request.agent_address
		seat.playerinfo.rolename=request.playerinfo.rolename
		seat.playerinfo.logo=request.playerinfo.logo
		seat.playerinfo.sex=request.playerinfo.sex
		seat.playerinfo.ip = request.playerinfo.ip
	elseif waitinfo ~= nil then
		waitinfo.gatesvr_id=request.gatesvr_id
		waitinfo.agent_address = request.agent_address
		waitinfo.playerinfo.rolename=request.playerinfo.rolename
		waitinfo.playerinfo.logo=request.playerinfo.logo
		waitinfo.playerinfo.sex=request.playerinfo.sex
		waitinfo.playerinfo.ip=request.playerinfo.ip		
	end

	if waitinfo == nil and seat == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求！"
		base.skynet_retpack(responsemsg, seatinfo)
		return
	end

	if table_data.is_had_start 
		and roomtablelogic.is_gameend(table_data) then
		if seat ~= nil then
			roomtablelogic.passive_standuptable(table_data, nil, seat, EStandupReason.STANDUP_REASON_UNKNOW)
			roomtablelogic.passive_leavetable(table_data, seat.rid, false)
		elseif waitinfo ~= nil then
			roomtablelogic.passive_leavetable(table_data, waitinfo.rid, false)			
		end		
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前游戏已经结束！"		
		base.skynet_retpack(responsemsg, nil)		
		return		
	end

	if waitinfo ~= nil then
		roomtablelogic.passive_leavetable(table_data, waitinfo.rid, false)
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前游戏已经结束！"		
		base.skynet_retpack(responsemsg, nil)		
		return		
	end

	if seat == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求！"
		base.skynet_retpack(responsemsg, seatinfo)
		return
	end

	responsemsg.gameinfo = {}
	msghelper:copy_table_gameinfo(responsemsg.gameinfo)
	base.skynet_retpack(responsemsg, seatinfo)
	roomtablelogic.reentertable(table_data, request, seat)	 
end

--[[
//请求离开桌子
message LeaveTableReq {
	optional Version version = 1;	
	optional int32 id = 2;
	optional string roomsvr_id = 3; //房间服务器id
	optional int32  roomsvr_table_address = 4; //桌子的服务器地址
	optional int32 gameid = 5;
}

//响应离开桌子
message LeaveTableRes {
	optional int32 errcode = 1; //错误原因 0表示成功
	optional string errcodedes = 2; //错误描述
	optional int32 gameid = 3;			
}
]]
function TableRequest.leavetable(request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid, 
	}
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")

	if table_data.conf == nil 
		or request.id ~= table_data.id 
		or request.gameid ~= table_data.conf.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效请求！"
		base.skynet_retpack(responsemsg)		
		return
	end
	seat = roomtablelogic.get_seat_by_rid(table_data, request.rid)

	if seat == nil then
		roomtablelogic.leavetable(table_data, request, seat)
		base.skynet_retpack(responsemsg)		
		return
	end
	----如果玩家在游戏中,不能让他能够退出房间
	if not roomtablelogic.is_gameend(table_data) 
						and table_data.is_had_start then
		responsemsg.errcode = EErrCode.ERR_PLAYER_IN_GAME
		responsemsg.errcodedes = "房间正在游戏中,不能退出！"
		base.skynet_retpack(responsemsg)
		return
	end

	roomtablelogic.standuptable(table_data, request, seat)
	roomtablelogic.leavetable(table_data, request, seat)	
	base.skynet_retpack(responsemsg)		
end

--[[
//请求坐入桌子
message SitdownTableReq {
	optional Version version = 1;
	optional int32 id = 2;
	optional string roomsvr_id = 3; //房间服务器id
	optional int32  roomsvr_table_address = 4; //桌子的服务器地址
	optional int32  roomsvr_seat_index = 5; //指定桌位号
	optional int32 gameid = 6;
}

//响应坐入桌子
message SitdownTableRes {
	optional int32 errcode = 1; //错误原因 0表示成功
	optional string errcodedes = 2; //错误描述
	optional int32 gameid = 3;	
}
]]
function TableRequest.sitdowntable(request)
 	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS, 
		gameid = request.gameid,
	}
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seatinfo, seat
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")

	if table_data.conf == nil 
		or request.gameid ~= table_data.conf.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效请求！"
		base.skynet_retpack(responsemsg)
		return
	end

	seat = roomtablelogic.get_seat_by_rid(table_data, request.rid)

	if seat == nil 
		and table_data.is_had_start 
		and roomtablelogic.is_gameend(table_data) then
		if seat ~= nil then
			roomtablelogic.passive_standuptable(table_data, nil, seat, EStandupReason.STANDUP_REASON_UNKNOW)
			roomtablelogic.passive_leavetable(table_data, seat.rid, false)
		elseif waitinfo ~= nil then
			roomtablelogic.passive_leavetable(table_data, request.rid, false)			
		end		
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前游戏已经结束, 不能坐下"		
		base.skynet_retpack(responsemsg)		
		return		
	end

	if seat ~= nil then
		seatinfo = {
			index = seat.index
		}
		seat.gatesvr_id=request.gatesvr_id
		seat.agent_address = request.agent_address
		seat.playerinfo.rolename=request.playerinfo.rolename
		seat.playerinfo.logo=request.playerinfo.logo
		seat.playerinfo.sex=request.playerinfo.sex
		seat.playerinfo.ip = request.playerinfo.ip

		base.skynet_retpack(responsemsg, seatinfo)
		return
	else
		if roomtablelogic.is_full(table_data) then
			responsemsg.errcode = EErrCode.ERR_TABLE_FULL
			responsemsg.errcodedes = "当前桌子已经满了！"
			base.skynet_retpack(responsemsg, seatinfo)
			return
		end

		if request.roomsvr_seat_index == 0 then
			seat = roomtablelogic.get_emptyseat_by_index(table_data, nil)
		else
			seat = roomtablelogic.get_emptyseat_by_index(table_data, request.roomsvr_seat_index)
		end

		if seat == nil then
			responsemsg.errcode = EErrCode.ERR_NO_EMPTY_SEAT
			responsemsg.errcodedes = "当前桌子没有空座位了！"
			base.skynet_retpack(responsemsg, seatinfo)
			return			
		end
		if seat.state ~= ENNSeatState.SEAT_STATE_NO_PLAYER then
			responsemsg.errcode = EErrCode.ERR_NOT_NO_PLAYER
			responsemsg.errcodedes = "座位不是没有玩家的状态！"
			base.skynet_retpack(responsemsg, seatinfo)
		end
		seatinfo = {
			index = seat.index,
		}

		--增加桌子人数计数 
		table_data.sitdown_player_num = table_data.sitdown_player_num + 1		
	end
	base.skynet_retpack(responsemsg, seatinfo)

	roomtablelogic.sitdowntable(table_data, request, seat)

end

--[[
//请求从桌子站起
message StandupTableReq {
	optional Version version = 1;
	optional int32 id = 2;
	optional string roomsvr_id = 3; //房间服务器id
	optional int32  roomsvr_table_address = 4; //桌子的服务器地址
	optional int32 gameid = 5;
}

//响应从桌子站起
message StandupTableRes {
	optional int32 errcode = 1; //错误原因 0表示成功
	optional string errcodedes = 2; //错误描述
	optional int32 gameid = 3;		
}
]]
function TableRequest.standuptable(request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid, 
	}
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")

	if table_data.conf == nil 
		or table_data.conf.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效请求！"
		base.skynet_retpack(responsemsg)
		return
	end

	if not roomtablelogic.is_gameend(table_data) then
		responsemsg.errcode = EErrCode.ERR_NOTCAN_LEAVETABLE
		responsemsg.errcodedes = "游戏已经开始，当前不能从桌子上站起！"
		base.skynet_retpack(responsemsg)		
		return
	end


	seat = roomtablelogic.get_seat_by_rid(table_data, request.rid)

	if seat == nil then
		responsemsg.errcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.errcodedes = "你已经站起了！"
		base.skynet_retpack(responsemsg)
		return
	end	

	base.skynet_retpack(responsemsg)

	roomtablelogic.standuptable(table_data, request, seat)
end
--[[
//桌主请求开始游戏
message StartGameReq {
	optional Version version = 1;	
	optional int32 id = 2;
	optional string roomsvr_id = 3; //房间服务器id
	optional int32  roomsvr_table_address = 4; //桌子的服务器地址
	optional int32 gameid = 5;	
}

//响应桌主开始游戏
message StartGameRes {
	optional int32 errcode = 1; //错误原因 0表示成功
	optional string errcodedes = 2; //错误描述
	optional int32 gameid = 3;		
}
]]
function TableRequest.startgame(request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid, 
	}
	local server = msghelper:get_server()
	local table_data = server.table_data

	if table_data.conf == nil 
		or table_data.conf.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效请求！"
		base.skynet_retpack(responsemsg)
		return		
	end

	if table_data.state ~= ENNTableState.TABLE_STATE_WAIT_GAME_START then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效请求！"
		base.skynet_retpack(responsemsg)
		return		
	end
	base.skynet_retpack(responsemsg)
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	roomtablelogic.startgame(table_data, request)
end

function TableRequest.doaction(request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid, 
	}
	local server = msghelper:get_server()
	local table_data = server.table_data
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	local roomseatlogic = logicmng.get_logicbyname("roomseatlogic")
	local seat = roomtablelogic.get_seat_by_rid(table_data, request.rid)
	
	if table_data.conf == nil 
		or table_data.conf.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效请求！"
		base.skynet_retpack(responsemsg)
		return		
	end

	if seat == nil then
		responsemsg.errcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.errcodedes = "玩家不在座位上！"
		base.skynet_retpack(responsemsg)
		return
	end

	if table_data.action_player_list == nil 
		or table_data.action_player_list[request.rid] == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "您不能操作!!!!!"
		base.skynet_retpack(responsemsg)		
		return
	end

	if  table_data.action_player_list[request.rid].action_state ~= 0 
		or table_data.action_player_list[request.rid].action_types == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前无效的操作类型!!!!!"
		base.skynet_retpack(responsemsg)		
		return
	end

	local is_valid_action_type = false
	for _, action_type in ipairs(table_data.action_player_list[request.rid].action_types) do
		if action_type == request.action_type then
			is_valid_action_type = true
			break
		end
	end
	if not is_valid_action_type then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前无效的操作类型!!!!!"
		base.skynet_retpack(responsemsg)		
		return
	end

	if request.action_type == ENNActionType.ACTION_TYPE_PINGNIU then
		local is_find = false
		for _, card in ipairs(seat.cards) do
			if request.cards ~= nil 
				and request.cards[1] ~= nil
				and card == request.cards[1] then
				is_find = true
				break
			end
		end 
		if not is_find or #(request.cards) > 3 then
			responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.errcodedes = "当前牌无牛"
			base.skynet_retpack(responsemsg)		
			return
		end

		--判断是否可以拼牛
		if nncardtool.get_niu_cards(request.cards) == nil then
			responsemsg.errcode = EErrCode.ERR_INVALID_WU_NIU
			responsemsg.errcodedes = "当前牌无牛"
			base.skynet_retpack(responsemsg)		
			return			
		end
	elseif request.action_type == ENNActionType.ACTION_TYPE_WUPINGNIU then
		--判断是否可以拼牛
		if seat.card_type ~= ENNBaseCardType.NNBASE_CARD_TYPE_UNKNOW then
			responsemsg.errcode = EErrCode.ERR_INVALID_YOU_NIU
			responsemsg.errcodedes = "你当前有牛, 请仔细检查哟"
			base.skynet_retpack(responsemsg)
			return
		end
	end
	
	base.skynet_retpack(responsemsg)
	roomtablelogic.doaction(table_data, request, seat)		
end

function TableRequest.gameready(request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid
	}
	local server = msghelper:get_server()
	local table_data = server.table_data
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	local seat = roomtablelogic.get_seat_by_rid(table_data, request.rid)
	if table_data.conf == nil 
		or seat == nil 
		or  table_data.conf.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.errcodedes = "玩家不在座位上！"
		base.skynet_retpack(responsemsg)
		return
	end

	if table_data.state ~= ENNTableState.TABLE_STATE_WAIT_ALL_READY then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前房间状态玩家不能举手!!!!!"
		base.skynet_retpack(responsemsg)		
		return
	end

	if seat.state ~= ENNSeatState.SEAT_STATE_WAIT_READY then
		responsemsg.errcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.errcodedes = "座位状态不是等待准备状态！"
		base.skynet_retpack(responsemsg)
		return
	end
	---如果房间
	seat.state = ENNSeatState.SEAT_STATE_WAIT_START
	base.skynet_retpack(responsemsg)
	roomtablelogic.gameready(table_data, request, seat)
end


--- 玩家发送桌内聊天消息
-- @param request
--
function TableRequest.sendtablemessage(request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}
	local server = msghelper:get_server()
	local table_data = server.table_data
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	local seat = roomtablelogic.get_seat_by_rid(table_data, request.rid)
	local waitplayer = roomtablelogic.get_waitplayer_by_rid(table_data, request.rid)
	
	if table_data.conf == nil 
		or table_data.conf.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的游戏类型！"
		base.skynet_retpack(responsemsg)
		return
	end

	if seat == nil and waitplayer == nil then
		responsemsg.errcode = EErrCode.ERR_NOT_INTABLE
		responsemsg.errcodedes = "你已经不在桌位上！"
		base.skynet_retpack(responsemsg)
		return
	end
	------向房间内的玩家广播消息
	roomtablelogic.sendmessage(table_data, request)
	base.skynet_retpack(responsemsg)
end

function TableRequest.getcurrgameboardrecord(request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid
	}
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.conf == nil 
		or table_data.conf.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求， 游戏id无效！"
		base.skynet_retpack(responsemsg)
		return
	end

	if table_data.state ~= ENNTableState.TABLE_STATE_UNKNOW then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前房间还未计算战绩!!!!!"
		base.skynet_retpack(responsemsg)		
		return
	end

	base.skynet_retpack(responsemsg)		
	--[[local noticemsg = {
		create_user_rid = table_data.conf.create_user_rid,
	    create_time = table_data.conf.create_time,
		terminal_time = table_data.terminal_time, 
	    create_user_rolename = table_data.conf.create_user_rolename,
	    create_table_id = table_data.conf.create_table_id,
	    create_user_logo = table_data.conf.create_user_logo,
		playerrecordlist = {}
	}

	local playerrecorditem
	for _, seat in ipairs(table_data.seats) do
		playerrecorditem = {
			rid = seat.rid,
			dianpaonum = seat.dianpao_count,
			middlebankernum = seat.middlebanker_count,
			hupainum = seat.hupai_count,
			total_huxi = seat.total_huxi,
			rolename = seat.playerinfo.rolename,
			logo = seat.playerinfo.logo,
		}
		table.insert(noticemsg.playerrecordlist, playerrecorditem)
	end]]

	msgproxy.sendrpc_noticemsgto_gatesvrd(request.gatesvr_id, request.agent_address, "NNGameRecordNtc", table_data.gamerecords)

end

function TableRequest.applydisband(request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}

	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.conf == nil 
		or table_data.conf.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求， 游戏id无效！"
		base.skynet_retpack(responsemsg)		
		return
	end

	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")

	if table_data.is_had_start and roomtablelogic.is_gameend(table_data) then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "游戏已经结束，请求无效！"
		base.skynet_retpack(responsemsg)
		return				
	end

	if table_data.playerdisbandlist ~= nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前已经处于等待解散状态，请求无效！"
		base.skynet_retpack(responsemsg)
		return						
	end

	local seat = roomtablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "请求无效，您不在座位上，不能进行该操作"
		base.skynet_retpack(responsemsg)
		return
	end

	--游戏没有开始
	if not table_data.is_had_start then

		base.skynet_retpack(responsemsg,
			 {retain_to_num=table_data.conf.retain_to_num, gameid=table_data.conf.gameid, create_table_id=table_data.conf.create_table_id})

		local noticemsg = {
				rid = request.rid,
				rolename = request.playerinfo.rolename,
				logo = request.playerinfo.logo,
				is_had_start = 0,
		}
		msghelper:sendmsg_to_alltableplayer("ApplyDisbandNtc", noticemsg)

		table_data.is_had_start = true
		table_data.state = ENNTableState.TABLE_STATE_UNKNOW

		msgproxy.sendrpc_broadcastmsgto_tablesvrd("delete", table_data.conf.gameid, table_data.svr_id , table_data.id)
		--检查桌子当前是否能够删除
		if table_data.delete_table_timer_id <= 0 then
			table_data.delete_table_timer_id = -1
		end
		table_data.delete_table_timer_id = timer.settimer(30*100, "delete_table")
		return
	end

	base.skynet_retpack(responsemsg)

    local noticemsg = {
    	rid = request.rid,
		rolename = request.playerinfo.rolename,
		logo = request.playerinfo.logo,
		is_had_start = 1,
		apply_disband_countdown = configdao.get_businessconfitem_by_index(100, 1000, "globalvarcfg",   "apply_disband_countdown"),
	}

	table_data.apply_disband_time = timetool.get_time()
	noticemsg.playerdisbandlist = {}
	noticemsg.surplus_applydisband_countdown = noticemsg.apply_disband_countdown - (timetool.get_time() - table_data.apply_disband_time)

	if table_data.disband_table_timer_id > 0 then
		timer.cleartimer(table_data.disband_table_timer_id)
		table_data.disband_table_timer_id = -1
	end
	table_data.disband_table_timer_id = timer.settimer(noticemsg.apply_disband_countdown*100, "applydisband")
	if roomtablelogic.is_gameend(table_data) then
	   timer.cleartimer(table_data.disband_table_timer_id)
	   table_data.disband_table_timer_id = -1
	   return
	end

	local roomseatlogic = logicmng.get_logicbyname("roomseatlogic")

	for index, seat in ipairs(table_data.seats) do
		if roomseatlogic.is_empty(seat) then
			if seat.rid ~= request.rid then
				local playerdisbanditem = {
					rid = seat.rid,
					rolename = seat.playerinfo.rolename,
					logo = seat.playerinfo.logo,
					agree_state = 0,
				}
				table.insert(noticemsg.playerdisbandlist, playerdisbanditem)
			else
				table_data.applydisband_seat_index = index
			end
		end
	end

	table_data.playerdisbandlist = noticemsg.playerdisbandlist

	msghelper:sendmsg_to_alltableplayer("ApplyDisbandNtc", noticemsg)
end

function TableRequest.agreeapplydisband(request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}

	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.conf == nil 
		or table_data.conf.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求， 游戏id无效！"
		base.skynet_retpack(responsemsg)		
		return
	end

	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")

	if table_data.is_had_start and roomtablelogic.is_gameend(table_data) then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "游戏已经结束，请求无效！"
		base.skynet_retpack(responsemsg)
		return				
	end

	if not table_data.is_had_start then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "游戏已经没有开始，请求无效！"
		base.skynet_retpack(responsemsg)
		return						
	end

	if table_data.playerdisbandlist == nil or table_data.applydisband_seat_index == 0 then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前不处于等待解散状态，请求无效！"
		base.skynet_retpack(responsemsg)
		return								
	end 

	local is_find = false
	local is_all_agree = true
	for _, playerdisbanditem in ipairs(table_data.playerdisbandlist) do
		if playerdisbanditem.rid == request.rid then
			is_find = true
			if playerdisbanditem.agree_state ~= 0 then
				responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
				responsemsg.errcodedes = "你已经操作过了，请求无效！"
				base.skynet_retpack(responsemsg)
				return										
			else
				playerdisbanditem.agree_state = request.is_agree				
			end
		end

		if playerdisbanditem.agree_state ~= 1 then
			is_all_agree = false
		end
	end
	
	if not is_find then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前你不能操作，请求无效！"
		base.skynet_retpack(responsemsg)
		return						
	end

	base.skynet_retpack(responsemsg)

	local noticemsg = {
    	rid = table_data.seats[table_data.applydisband_seat_index].rid,
		rolename = table_data.seats[table_data.applydisband_seat_index].playerinfo.rolename,
		logo = table_data.seats[table_data.applydisband_seat_index].playerinfo.logo,
		is_had_start = 1,
		playerdisbandlist = table_data.playerdisbandlist,
		apply_disband_countdown = configdao.get_businessconfitem_by_index(100, 1000, "globalvarcfg",   "apply_disband_countdown"),
	}
	noticemsg.surplus_applydisband_countdown = noticemsg.apply_disband_countdown - (timetool.get_time() - table_data.apply_disband_time)

	msghelper:sendmsg_to_alltableplayer("ApplyDisbandNtc", noticemsg)	

	if request.is_agree == 2 then
		if table_data.disband_table_timer_id > 0 then
			timer.cleartimer(table_data.disband_table_timer_id)
			table_data.disband_table_timer_id = -1
		end
		table_data.applydisband_seat_index = 0
		table_data.apply_disband_time = 0
		table_data.playerdisbandlist = nil
		return
	end

	if is_all_agree then
		if table_data.disband_table_timer_id > 0 then
			timer.cleartimer(table_data.disband_table_timer_id)
			table_data.disband_table_timer_id = -1
		end
		if table_data.gamerecords ~= nil then
			table_data.state = ENNTableState.TABLE_STATE_DISBAND_GAME
			local roomgamelogic = msghelper:get_game_logic()
			roomgamelogic.run(table_data.gamelogic)
		else
			--检查桌子当前是否能够删除
			if table_data.delete_table_timer_id > 0 then
				timer.cleartimer(table_data.delete_table_timer_id)
				table_data.delete_table_timer_id = -1
			end
			table_data.delete_table_timer_id = timer.settimer(400*100, "delete_table") 	
		end
	end
end

return TableRequest